<template>
  <div class="time-line">
    <div class="time-div">
      <!-- <div class="tip" :style="{ left: tipOffsetLeft + 'px' }">
        {{ tipValue }}
      </div> -->
      <div class="present-layer">
        <span>当前时间：{{curTime}} {{tipValue}}</span>
      </div>
      <div class="empty"></div>
      <div class="time-data">
        <div class="line-circle" :style="{ width: lineWidth + 'px', left: offsetLeft + 'px' }">
          <div class="item" v-for="(option, i) in timeData" :key="i" :style="{ width: stepLength + 'px' }">
            <div class="time-mark" @click="changeTime(i, option)" :class="i <= timeIndex ? 'active' : ''" v-if="i % markStep == 0">
              <div class="aa"></div>
              <div class="markValue">{{ option }}</div>
            </div>
            <div class="time-circle" :class="i <= timeIndex ? 'active' : ''" @click="changeTime(i, option)" v-if="i % markStep"></div>
            <div class="cylinder" v-if="i != timeData.length - 1"></div>
          </div>
          <div class="progress" :style="{ width: processWidth + 'px' }"></div>
        </div>
      </div>
      <div class="time-btn">
        <div class="btn" @click="playEvent">
          <img src="@/assets/image/time/ic_zanting.png" alt="" v-show="playStart" />
          <img src="@/assets/image/time/ic_bofang.png" alt="" v-show="!playStart" />
        </div>
        <div class="btn" @click="resetEvent">
          <img src="@/assets/image/time/ic_chongzhi.png" alt="" />
        </div>
        <div class="btn close-btn" @click="close">
          <span>x</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/****
 * 时间轴组件
 * timeData:[Array] 时间数组
 * markStep :number //不传递默认取平均间5个标记
 * interval:number //不传递默认取5秒
 * autoplay:Boole // 默认关闭
 * limit:number //一页展示几个,不设置默认全展示
 * stepLength:number //步长
 * loop:Boole
 ****/
export default {
  data() {
    return {
      lineWidth: null,
      timeIndex: 0,
      timer: null,
      processWidth: 0,
      tipOffsetLeft: 50,
      tipValue: "",
      offsetLeft: 0,
      defaultOffsetLeft: 50,
      playStart: false,
      curTime: null
    };
  },
  props: {
    timeData: {
      type: Array,
      default: () => [
        "208-02 09:00",
        "11:02",
        "13:03",
        "08-10 09:04",
        "208-02 09:00",
        "11:02",
        "13:03",
        "08-10 09:04",
      ],
    },
    nameData: {
      type: Array,
      default: () => [
        "208-02 09:00",
        "11:02",
        "13:03",
        "08-10 09:04",
        "208-02 09:00",
        "11:02",
        "13:03",
        "08-10 09:04",
      ],
    },
    markStep: {
      type: Number,
      default: 5,
    },
    interval: {
      type: Number,
      default: 1,
    },
    autoplay: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Number,
      default: 0,
    },
    stepLength: {
      type: Number,
      default: 25,
    },
    loop: {
      type: Boolean,
      default: true,
    },
  },
  watch: {
    autoplay: function (val) {
      val ? this.play() : this.stop();
    },
    playStart(newVal) {
      if (newVal) {
        this.$emit("changeTime", 'change', this.curTime, this.timeIndex);
      } else {
        this.$emit('stopTime', this.curTime, this.timeIndex);
      }
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init: function () {
      this.tipValue = this.nameData[0];
      this.lineWidth = this.limit ? (this.limit - 1) * this.stepLength + 10 : (this.timeData.length - 1) * this.stepLength + 50;
      this.autoplay ? this.play() : null;
      this.getCurTime();
    },
    play: function () {
      this.setTimePlay();
      this.playStart = true;
      this.playCreate();
    },
    stop: function () {
      this.playStart = false;
      this.timer ? clearInterval(this.timer) : null;
    },
    playCreate: function () {
      let vm = this;
      let dataLength = this.timeData.length;
      vm.timer ? clearInterval(vm.timer) : null;

      /***
       * 判断两种状态
       * 1、需要滚动的
       * 2、不需要滚动的
       */
      vm.timer = setInterval(function () {
        vm.timeIndex++;
        if (dataLength <= vm.timeIndex) {
          if (vm.loop) {
            vm.timeIndex = 0;
            vm.offsetLeft = 0;
            vm.setTimePlay();
          } else {
            vm.playStart = false;
            clearInterval(vm.timer);
          }
        } else {
          vm.setTimePlay();
        }
      }, vm.interval * 1000);
    },
    setTimePlay: function (offset = true) {
      let limitIndex = this.limit
        ? this.timeIndex % this.limit
        : this.timeIndex;
      this.tipValue = this.nameData[this.timeIndex];
      this.processWidth = this.stepLength * this.timeIndex;
      if (offset) {
        if (!limitIndex) {
          if (this.timeIndex) {
            this.offsetLeft -= this.stepLength * this.limit;
          } else {
            this.offsetLeft = 0;
          }
        }
      }
      this.tipOffsetLeft = this.defaultOffsetLeft + this.stepLength * limitIndex;
      this.$emit("changeTime", 'change', this.curTime, this.timeIndex);
    },
    playEvent: function () {
      this.playStart = !this.playStart;
      if (this.playStart) {
        //开始
        this.playCreate();
      } else {
        //暂停
        this.timer ? clearInterval(this.timer) : null;
      }
    },
    resetEvent: function () {
      this.timeIndex = 0;
      this.setTimePlay();
      this.stop();
    },
    changeTime: function (i) {
      this.stop();
      this.timeIndex = i;
      this.setTimePlay(false);
      this.$emit("changeTime", 'skip', this.curTime, this.timeIndex);
    },
    animate: function (cb, time) {
      let i = 1;
      let vm = this;
      vm.timer = requestAnimationFrame(function fn() {
        if (!(i % parseInt(60 / (1000 / time)))) {
          cb();
        }
        i++; // 记录requestAnimationFrame执行的次数
        vm.timer = requestAnimationFrame(fn); // 开启下次requestAnimationFrame
      });
    },
    getCurTime() {
      let date = new Date(),
        year = date.getFullYear(), // 获取完整的年份(4位)
        month = date.getMonth() + 1, // 获取当前月份(0-11,0代表1月)
        strDate = date.getDate(); // 获取当前日(1-31)
      if (month < 10) month = `0${month}`; // 如果月份是个位数，在前面补0
      if (strDate < 10) strDate = `0${strDate}`; // 如果日是个位数，在前面补0
      this.curTime = `${year}/${month}/${strDate}`;
    },
    refreshTimeline(timeData) {
      this.timeData = timeData;
    },

    close() {
      this.$emit('close');
    }
  },
};
</script>

<style lang="less" scoped>
.time-line {
  width: 100%;
  height: 100%;
  transform-origin: center bottom;
  background: url(~@/assets/image/time/border_time.png) no-repeat;
  background-size: 100% 100%;
  z-index: 15;
  display: flex;
  padding-top: 20px;
  z-index: 1200;
  pointer-events: all;
}

.time-div {
  height: 100%;
  display: flex;
  justify-content: space-between;
  overflow: hidden;

  .time-data {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    position: relative;
    overflow: hidden;
    transition: all 1s ease-in-out;

    .line-circle {
      display: flex;
      justify-content: space-between;
      height: 90%;
      padding-left: 20px;
      position: relative;
      flex-shrink: 0;
      // top: -10px;
      transition: all 1s ease-in-out;
      box-sizing: border-box;
      overflow-x: auto;

      .progress {
        width: 0px;
        height: 5px;
        top: calc(50% - 2.5px);
        position: absolute;
        transition: all 1s ease-in-out;
        background-color: rgba(77, 207, 255, 1);
      }

      .item {
        width: 35px;
        display: flex;
        align-items: center;
        flex-shrink: 0;
        position: relative;

        .time-mark {
          cursor: pointer;
          height: 15px;
          width: 15px;
          background-color: #57758f;
          z-index: 3;
          display: flex;
          border-radius: 100%;
          align-items: center;
          justify-content: center;
          position: absolute;
          left: 25px;
          transform: translatex(-30px);

          .aa {
            height: 15px;
            width: 15px;
            border-radius: 50%;
            background-color: #a1c0ff;
            z-index: 4;
          }

          .markValue {
            position: absolute;
            top: 18px;
            font-size: 12px;
            color: #fff;
            white-space: nowrap;
            width: 100%;
          }
        }

        .active {
          background-color: rgba(77, 207, 255, 1) !important;
          transition: all 0.2s ease-in-out 0.5s;
        }

        .time-circle {
          cursor: pointer;
          height: 20px;
          width: 5px;
          background-color: #57758f;
          z-index: 3;
          display: flex;
          align-items: center;
          justify-content: center;
          position: absolute;
        }

        .cylinder {
          height: 5px;
          width: 100%;
          display: flex;
          background-color: rgba(77, 207, 255, 0.6);
        }
      }
    }
  }

  .time-btn {
    height: 100%;
    margin: 28px 30px 0 8px;
    display: flex;
    // align-items: center;
    justify-content: space-around;

    .btn {
      cursor: pointer;
      height: 23px;
      width: 23px;
      background-color: rgba(0, 168, 255, 0.5);
      border: 2px solid none;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0px 5px;
      // margin-top: -23px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .close-btn {
      padding-bottom: 5px;
      box-sizing: border-box;
      font-size: 16px;
      color: #fff;
      font-weight: 400;
    }
  }

  .present-layer {
    position: absolute;
    top: 15px;
    left: 35px;
    font-size: 16px;
    color: #fff;
  }

  .tip {
    position: absolute;
    font-size: 16px;
    top: -50px;
    padding: 10px 10px;
    background-color: rgba(56, 118, 210, 60%);
    border-radius: 0%;
    transition: all 1s ease-in-out;
    white-space: nowrap;
    transform: translateX(-50%);
    color: #fff;

    &::after,
    &::before {
      border: solid transparent;
      content: ' ';
      height: 0;
      top: 100%;
      position: absolute;
      width: 0;
    }

    &::before {
      border-width: 12px;
      border-top-color: rgba(56, 118, 210, 60%);
      left: calc(50% - 12px);
    }
  }

  .empty {
    width: 30px;
  }
}
</style>
